package client.global.commlib

import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager

/**
 * Date: 2022/3/18 19:10
 * Description:配置文件
 */
class AppConfig_c private constructor() {

    private object SingleHolder {
        val sInstance = AppConfig_c()
    }

    companion object {
        @JvmStatic
        val instance: AppConfig_c
            get() = SingleHolder.sInstance
    }


    var HOST: String = getHost()

    var HOST_PORT: String = getHostPort()

    /**
     * 获取域名
     */
    fun getHost(): String {
        return getMetaDataString("SERVER_HOST")
    }

    /**
     * 获取域名 + 端口号
     */
    fun getHostPort(): String {
        return getMetaDataString("SERVER_HOST_PORT")
    }

    /**
     * 根据key  获取清单文件的 值
     */
    fun getMetaDataString(key: String): String {
        var res: String = ""
        try {
            val appInfo: ApplicationInfo = BaseApplication.getInstance().packageManager
                .getApplicationInfo(
                    BaseApplication.getInstance().packageName,
                    PackageManager.GET_META_DATA
                )
            res = appInfo.metaData.getString(key).toString()
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return res
    }

    /**
     * 根据key  获取清单文件的 值
     */
    fun getMetaDataInt(key: String): Int {
        var res: Int = 0
        try {
            val appInfo: ApplicationInfo = BaseApplication.getInstance().packageManager
                .getApplicationInfo(
                    BaseApplication.getInstance().packageName,
                    PackageManager.GET_META_DATA
                )
            res = appInfo.metaData.getInt(key)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return res
    }

    /**
     * 获取前versionCode
     *
     * @return
     */
    val versionCode: Int
        get() {
            try {
                return BaseApplication.getInstance().packageManager
                    .getPackageInfo(BaseApplication.getInstance().packageName, 0).versionCode
            } catch (e1: NumberFormatException) {
                e1.printStackTrace()
            } catch (e1: PackageManager.NameNotFoundException) {
                e1.printStackTrace()
            }
            return 0
        }

    /**
     * 获取versionName
     *
     * @return
     */
    val versionName: String
        get() {
            try {
                return BaseApplication.getInstance().packageManager
                    .getPackageInfo(BaseApplication.getInstance().packageName, 0).versionName
            } catch (e1: NumberFormatException) {
                e1.printStackTrace()
            } catch (e1: PackageManager.NameNotFoundException) {
                e1.printStackTrace()
            }
            return "1.01"
        }


//    /**
//     * 获取手机号
//     */
//    fun getMoblie(): String {
//        var userinfoBean = GsonUtils.fromJson(spLocalUserInfo, UserinfoBean::class.java)
//        return userinfoBean.mobile
//    }



}